package org.istt.web;

import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.opensymphony.xwork2.conversion.TypeConversionException;

public class JodaDateConverter extends DateConverter {
	
	@Override
    public Object convertFromString(Map ctx, String[] value, Class arg2) {
        if (value[0] == null || value[0].trim().equals("")) {
            return null;
        }
        try {
        	DateTimeFormatter fmt = DateTimeFormat.forPattern(getFormat());
        	return fmt.parseDateTime(value[0]);
        } catch (Exception pe) {
            throw new TypeConversionException("Invalid date!");
        }
    }
    
	@Override
    public String convertToString(Map ctx, Object data) {
        return ((DateTime)data).toString(getFormat());
    }
} 